package com.zyx.xunyilu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyx.xunyilu.config.jwt.JWTUtils;
import com.zyx.xunyilu.entity.User;
import com.zyx.xunyilu.enums.RegisterStatus;
import com.zyx.xunyilu.service.UserService;
import com.zyx.xunyilu.utils.CacheUtils;
import com.zyx.xunyilu.utils.EmailUtils;
import com.zyx.xunyilu.utils.RestResponse;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName: AdminController
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2020/4/15 2:35
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    private String host;
    private final JWTUtils jwtUtils;
    private final UserService userService;
    private final EmailUtils emailUtils;
    private final CacheUtils cacheUtils;

    @Autowired
    public AdminController(JWTUtils jwtUtils, UserService userService, EmailUtils emailUtils, CacheUtils cacheUtils) {
        this.jwtUtils = jwtUtils;
        this.userService = userService;
        this.emailUtils = emailUtils;
        this.cacheUtils = cacheUtils;
    }

    @ModelAttribute
    public void setToken(HttpServletRequest request,
                         @RequestHeader(value = "token", required = false, defaultValue = "") String headerToken,
                         @RequestParam(value = "token", required = false) String pToken) {
        StringBuffer requestURL = request.getRequestURL();
        int i = requestURL.indexOf("/", 8);
        if (i != -1) {
            host = requestURL.substring(0, i);
        } else {
            host = String.valueOf(requestURL);
        }
        String token = headerToken;
        if (StringUtils.isEmpty(token)) {
            token = pToken;
        }
        request.setAttribute("token", token);
    }


    @RequiresRoles("admin")
    @GetMapping("/page")
    public RestResponse page(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                             User user) {
        return RestResponse.ok(userService.page(new Page<User>(page, size), user));
    }

    @RequiresRoles("admin")
    @GetMapping("/count")
    public RestResponse count() {
        return RestResponse.ok(userService.count());
    }

    @RequiresRoles("admin")
    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable Integer id) {
        User user = userService.deleteUserById(id);
        if (user != null) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @RequiresRoles("admin")
    @PostMapping("/delete/more")
    public RestResponse deleteMore(@RequestParam("ids[]") List<Integer> ids) {
        ids.forEach(userService::deleteUserById);
        return RestResponse.ok();
    }

    @RequiresRoles("admin")
    @PostMapping("/authorize/{id}")
    public RestResponse authorize(@PathVariable Integer id,
                                  @RequestParam("register") RegisterStatus registerStatus) {
        Integer authorize = userService.authorize(id, registerStatus);
        if (authorize > 0) {
            return RestResponse.ok();
        } else {
            return RestResponse.fail();
        }
    }

    @RequiresRoles("admin")
    @PostMapping("/authorize/more")
    public RestResponse authorizeMore(@RequestParam("ids[]") List<Integer> ids,
                                      @RequestParam("register") RegisterStatus registerStatus) {
        ids.forEach(t -> {
            userService.authorize(t, registerStatus);
        });
        return RestResponse.ok();
    }
}
